package com.ylbh.modules.cancellation.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.cancellation.dto.CancellationDTO;
import com.ylbh.common.pojo.member.vo.MemberVO;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.cancellation.entity.ShoppingUserCancellation;
import com.ylbh.modules.cancellation.mapper.ShoppingUserCancellationMapper;
import com.ylbh.modules.cancellation.service.ShoppingUserCancellationService;
import com.ylbh.modules.goodsorder.mapper.ShoppingOrderFormMapper;
import com.ylbh.modules.userauths.mapper.ShoppingUserAuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;


@Service("shoppingUserCancellationService")
public class ShoppingUserCancellationServiceImpl extends ServiceImpl<ShoppingUserCancellationMapper, ShoppingUserCancellation> implements ShoppingUserCancellationService {

    @Autowired
    private ShoppingUserCancellationMapper shoppingUserCancellationMapper;

    @Autowired
    private ShoppingOrderFormMapper shoppingOrderFormMapper;

    @Autowired
    private ShoppingUserAuthMapper shoppingUserAuthMapper;

    @Value("${url.clearUser}")
    private String clearUser;

    /**
     * 用户注销列表
     * @param dto
     * @return
     */
    @Override
    public PageUtils queryCancellationList(CancellationDTO dto) {
        Page<Object> page = new Page<>(dto.getPage(), dto.getLimit());
        List<ShoppingUserCancellation> list = shoppingUserCancellationMapper.selectCancellationList(dto, page);
        return new PageUtils(new Page<ShoppingUserCancellation>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 注销用户审核
     * @param dto
     * @return
     */
    @Override
    public String cancellationToExamine(CancellationDTO dto) {
        if (dto.getCancellationStatus() == 3) {
            // 后台人员通过用户的注销操作

            // 1.首先去查询这个用户是否还有未完成的订单
            int orderNum = shoppingOrderFormMapper.selectUnfinishedOrderByUserId(dto.getUserId());
            if (orderNum > 0) {
                return "失败";
            }

            // 2.删除第三方授权信息记录
            shoppingUserAuthMapper.updateUserAuthByUserId(dto.getUserId());

            // 查询出该用户的手机号
            MemberVO memberVO = shoppingUserAuthMapper.selectMobileByUserId(dto.getUserId());
            // 3.修改用户表中的用户信息，在手机号后加个a，并把上级修改成1
            shoppingUserAuthMapper.updateUserDetailsByUserId(dto.getUserId(), memberVO.getMobile(), dto.getCancellationStatus(), memberVO.getUserName());
        }
        // 修改注销表中的状态
        dto.setExamineTime(new Date());
        shoppingUserCancellationMapper.updateStatusById(dto);
        try {
            // 清除redis缓存
            String cleanUserUrl = clearUser + dto.getUserId();
            URL url = new URL(cleanUserUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "成功";
    }
}
